import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

import '../../util/ImConstant.dart';

class VideoPlayerPage extends StatefulWidget {
  final String videoPath;
  const VideoPlayerPage({ required this.videoPath, super.key});

  @override
  State<StatefulWidget> createState() => _VideoPlayerState();

}

class _VideoPlayerState extends State<VideoPlayerPage> {

  late VideoPlayerController _controller;

  @override
  void initState() {
    super.initState();

    _controller = VideoPlayerController.file(File(widget.videoPath))
      ..initialize().then((_) {
        setState(() {});
      });
    _controller.play();
  }

  @override
  Widget build(BuildContext context) {
     return Scaffold(
       appBar: _buildAppBar(context),
       body: _controller.value.isInitialized ? OrientationBuilder(
         builder: (context, orientation) {
           return AspectRatio(
             aspectRatio: _controller.value.aspectRatio,
             child: VideoPlayer(_controller),
           );
         },
       )
           : Container(),
     );
  }

  AppBar _buildAppBar(BuildContext context) {
    return AppBar(
      backgroundColor: ImConstant.IM_MAIN_BACKGROUND_COLOR,
      leading: GestureDetector(
        child: const Padding(
          padding: EdgeInsets.only(left: 15, right: 15),
          child: Icon(Icons.arrow_back_rounded),
        ),
        onTap: () => Navigator.of(context).pop(),
      ),
      titleSpacing: 5,
      title: const Text('视频查看', style: TextStyle(color: Colors.black, fontSize: ImConstant.IM_DEFAULT_SIZE),),
    );
  }
}